namespace SinaFinance.ChartLibrary.StockTopFiveChangesLibrary
{
    using SinaFinance.CtrlDelegate;
    using SinaFinanceBaseClassLibrary;
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Runtime.CompilerServices;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Xml.Linq;

    public partial class StockTopFiveChanges : UserControl
    {
        //private bool _contentLoaded;
        private string buyText = string.Empty;
        private List<double> buyTopAmount5 = new List<double>();
        private List<string> buyTopPrice5 = new List<string>();
        private string currentPrice = string.Empty;
        private const double GRIDROWHEIGHT = 20.0;
        private int GridRowIndex = 0;
        private const double HistogramAreaPercent = 0.75;
        private double HistogramAreaWidth = 0.0;
        private const double HISTOGRAMHEIGHT = 8.0;
        private int index = 0;
        //internal Grid LayoutRoot;
        private string priceValue = string.Empty;
        private string sellText = string.Empty;
        private List<double> sellTopAmount5 = new List<double>();
        private List<string> sellTopPrice5 = new List<string>();
        private List<Canvas> StockTopFiveChangesControls = new List<Canvas>();
        private List<TextBlock> StockTopFiveChangesControls2 = new List<TextBlock>();
        private const double TextAreaPercent = 0.25;
        private string title = string.Empty;

        public StockTopFiveChanges()
        {
            this.InitializeComponent();
            if (base.Width <= 0.0)
            {
                base.Width = 300.0;
            }
            if (base.Height <= 0.0)
            {
                base.Height = 400.0;
            }
            base.Loaded += new RoutedEventHandler(this.StockTopFiveChanges_Loaded);
        }

        protected void BindStockTopFiveInfo()
        {
            if (this.sellTopAmount5.Count.Equals(5) && this.buyTopAmount5.Count.Equals(5))
            {
                int num7;
                List<double> list = this.InsetSort(this.sellTopAmount5);
                List<double> list2 = this.InsetSort(this.buyTopAmount5);
                double num = list[0];
                double num2 = list[(int) (list2.Count - 1)];
                this.HistogramAreaWidth = (base.Width * 0.75) - 100.0;
                double unitLen = 0.0;
                if (num2 > 0.0)
                {
                    unitLen = this.HistogramAreaWidth / num2;
                }
                double num4 = list2[0];
                double num5 = list2[(int) (list2.Count - 1)];
                double num6 = 0.0;
                if (num5 > 0.0)
                {
                    num6 = this.HistogramAreaWidth / num5;
                }
                for (num7 = this.StockTopFiveChangesControls.Count - 1; num7 > -1; num7--)
                {
                    this.LayoutRoot.Children.Remove(this.StockTopFiveChangesControls[num7]);
                }
                for (num7 = this.StockTopFiveChangesControls2.Count - 1; num7 > -1; num7--)
                {
                    this.LayoutRoot.Children.Remove(this.StockTopFiveChangesControls2[num7]);
                }
                this.RenderItemControl(StockTopFiveType.Buy, this.sellTopPrice5, this.sellTopAmount5, unitLen);
                this.RenderItemControl(StockTopFiveType.Sell, this.buyTopPrice5, this.buyTopAmount5, num6);
                this.RenderTitle(this.currentPrice, 5);
            }
        }

        private void histogram_REMOVESTOCKTIP(object sender, MouseEventArgs e)
        {
            Canvas canvas = (Canvas) sender;
            for (int i = canvas.Children.Count - 1; i > -1; i--)
            {
                if (canvas.Children[i].GetType().ToString().Equals("SinaFinance.ChartLibrary.StockTopFiveChangesLibrary.StockTopFiveTip"))
                {
                    canvas.Children.Remove(canvas.Children[i]);
                }
            }
        }

        private void histogram_SHOWSTOCKTIP(object sender, MouseEventArgs e)
        {
            StockTopFiveTip tip = new StockTopFiveTip();
            Canvas canvas = (Canvas) sender;
            canvas.Children.Add(tip);
            string content = canvas.Tag.ToString();
            tip.RenderTip(content);
            tip.Margin = new Thickness(canvas.Width + 10.0, -2.0, 0.0, 0.0);
        }

        //[DebuggerNonUserCode]
        //public void InitializeComponent()
        //{
        //    if (!this._contentLoaded)
        //    {
        //        this._contentLoaded = true;
        //        Application.LoadComponent(this, new Uri("/SinaFinance;component/ChartLibrary/StockTopFiveChangesLibrary/StockTopFiveChanges.xaml", UriKind.Relative));
        //        this.LayoutRoot = (Grid) base.FindName("LayoutRoot");
        //    }
        //}

        protected List<double> InsetSort(List<double> ary)
        {
            int num;
            List<double> list = new List<double>();
            for (num = 0; num < ary.Count; num++)
            {
                list.Add(ary[num]);
            }
            for (num = 0; num < list.Count; num++)
            {
                double num2 = list[num];
                int num3 = num;
                while ((num3 > 0) && (list[(int) (num3 - 1)] > num2))
                {
                    list[num3]= list[(int) (num3 - 1)];
                    num3--;
                }
                list[num3]= num2;
            }
            return list;
        }

        public void PostStockTopFiveResourceByInterval(DB_RealTime resource)
        {
            if (resource != null)
            {
                this.index++;
                this.sellTopPrice5.Clear();
                this.buyTopPrice5.Clear();
                this.sellTopAmount5.Clear();
                this.buyTopAmount5.Clear();
                this.buyTopPrice5.Add(resource.AskPrice1);
                this.buyTopPrice5.Add(resource.AskPrice2);
                this.buyTopPrice5.Add(resource.AskPrice3);
                this.buyTopPrice5.Add(resource.AskPrice4);
                this.buyTopPrice5.Add(resource.AskPrice5);
                this.buyTopAmount5.Add(Convert.ToDouble(resource.AskVolume1));
                this.buyTopAmount5.Add(Convert.ToDouble(resource.AskVolume2));
                this.buyTopAmount5.Add(Convert.ToDouble(resource.AskVolume3));
                this.buyTopAmount5.Add(Convert.ToDouble(resource.AskVolume4));
                this.buyTopAmount5.Add(Convert.ToDouble(resource.AskVolume5));
                this.sellTopPrice5.Add(resource.BidPrice1);
                this.sellTopPrice5.Add(resource.BidPrice2);
                this.sellTopPrice5.Add(resource.BidPrice3);
                this.sellTopPrice5.Add(resource.BidPrice4);
                this.sellTopPrice5.Add(resource.BidPrice5);
                this.sellTopAmount5.Add(Convert.ToDouble(resource.BidVolume1));
                this.sellTopAmount5.Add(Convert.ToDouble(resource.BidVolume2));
                this.sellTopAmount5.Add(Convert.ToDouble(resource.BidVolume3));
                this.sellTopAmount5.Add(Convert.ToDouble(resource.BidVolume4));
                this.sellTopAmount5.Add(Convert.ToDouble(resource.BidVolume5));
                this.BindStockTopFiveInfo();
            }
        }

        private void rectCanvas_MouseEnter(object sender, MouseEventArgs e)
        {
            StockTopFiveTip tip = new StockTopFiveTip();
            Canvas canvas = (Canvas) sender;
            canvas.Children.Add(tip);
            string content = canvas.Tag.ToString();
            tip.RenderTip(content);
            tip.Margin = new Thickness(canvas.Width + 5.0, -5.0, 0.0, 0.0);
        }

        private void rectCanvas_MouseLeave(object sender, MouseEventArgs e)
        {
            Canvas canvas = (Canvas) sender;
            for (int i = canvas.Children.Count - 1; i > -1; i--)
            {
                if (canvas.Children[i].GetType().ToString().Equals("SinaFinance.ChartLibrary.StockTopFiveChangesLibrary.StockTopFiveTip"))
                {
                    canvas.Children.Remove(canvas.Children[i]);
                }
            }
        }

        protected void RenderGridFramework()
        {
            ColumnDefinition definition = new ColumnDefinition {
                Width = new GridLength(base.Width)
            };
            this.LayoutRoot.RowDefinitions.Clear();
            this.LayoutRoot.ColumnDefinitions.Clear();
            this.LayoutRoot.Children.Clear();
            this.LayoutRoot.ColumnDefinitions.Add(definition);
        }

        protected void RenderItemControl(StockTopFiveType type, List<string> prices, List<double> amounts, double unitLen)
        {
            int num;
            RowDefinition definition;
            string str2;
            double num2;
            double num3;
            string str3;
            StockTopFiveChangesItem item;
            Canvas canvas;
            Color orange;
            Color color2;
            StockTopFiveHistogram histogram;
            Canvas canvas2;
            int num4;
            string buyText = string.Empty;
            if (type.Equals(StockTopFiveType.Buy))
            {
                this.GridRowIndex = 6;
                buyText = this.buyText;
                if (!prices.Count.Equals(0))
                {
                    this.priceValue = prices[0];
                    for (num = 0; num < prices.Count; num++)
                    {
                        definition = new RowDefinition {
                            Height = new GridLength(20.0)
                        };
                        this.LayoutRoot.RowDefinitions.Add(definition);
                        str2 = prices[num];
                        num2 = amounts[num];
                        num3 = unitLen * num2;
                        num4 = num + 1;
                        str3 = buyText.Replace("N", num4.ToString()) + " " + str2;
                        item = new StockTopFiveChangesItem {
                            Index = num,
                            StockPrice = str3
                        };
                        canvas = item.RenderStockTopFiveChangesItem();
                        canvas.Margin = new Thickness(10.0, (20.0 - item.ActualHeight) / 2.0, 0.0, 0.0);
                        this.LayoutRoot.Children.Add(canvas);
                        Grid.SetRow(canvas, this.GridRowIndex);
                        Grid.SetColumn(canvas, 0);
                        orange = Colors.Orange;
                        color2 = Colors.Orange;
                        if (type.Equals(StockTopFiveType.Sell))
                        {
                            orange = Colors.Orange;
                            color2 = Color.FromArgb(0xff, 0xf4, 0xe8, 0xa7);
                        }
                        else
                        {
                            orange = Color.FromArgb(0xff, 70, 130, 180);
                            color2 = Color.FromArgb(0xff, 0xa9, 0xc7, 0xff);
                        }
                        this.StockTopFiveChangesControls.Add(canvas);
                        histogram = new StockTopFiveHistogram {
                            HistogramAreaWidth = this.HistogramAreaWidth,
                            HistogramWidth = (num3 < 2.0) ? 2.0 : num3,
                            HistogramColor = orange,
                            HistogramLineColor = color2
                        };
                        if (this.index == 1)
                        {
                            histogram.IsLoad = true;
                        }
                        else
                        {
                            histogram.IsLoad = false;
                        }
                        canvas2 = new Canvas {
                            Width = this.HistogramAreaWidth,
                            Height = 20.0,
                            Tag = num2
                        };
                        canvas2.MouseEnter += new MouseEventHandler(this.rectCanvas_MouseEnter);
                        canvas2.MouseLeave += new MouseEventHandler(this.rectCanvas_MouseLeave);
                        canvas2.Margin = new Thickness(base.Width * 0.25, 6.0, 0.0, 0.0);
                        canvas2.Children.Add(histogram);
                        this.LayoutRoot.Children.Add(canvas2);
                        Grid.SetRow(canvas2, this.GridRowIndex);
                        Grid.SetColumn(canvas2, 0);
                        this.GridRowIndex++;
                    }
                }
            }
            else
            {
                this.GridRowIndex = 0;
                buyText = this.sellText;
                for (num = prices.Count - 1; num > -1; num--)
                {
                    definition = new RowDefinition {
                        Height = new GridLength(20.0)
                    };
                    this.LayoutRoot.RowDefinitions.Add(definition);
                    str2 = prices[num];
                    num2 = amounts[num];
                    num3 = unitLen * num2;
                    num4 = num + 1;
                    str3 = buyText.Replace("N", num4.ToString()) + " " + str2;
                    item = new StockTopFiveChangesItem {
                        Index = num,
                        StockPrice = str3
                    };
                    canvas = item.RenderStockTopFiveChangesItem();
                    canvas.Margin = new Thickness(10.0, (20.0 - item.ActualHeight) / 2.0, 0.0, 0.0);
                    this.LayoutRoot.Children.Add(canvas);
                    Grid.SetRow(canvas, this.GridRowIndex);
                    Grid.SetColumn(canvas, 0);
                    orange = Colors.Orange;
                    color2 = Colors.Orange;
                    if (type.Equals(StockTopFiveType.Sell))
                    {
                        orange = Colors.Orange;
                        color2 = Color.FromArgb(0xff, 0xf4, 0xe8, 0xa7);
                    }
                    else
                    {
                        orange = Color.FromArgb(0xff, 70, 130, 180);
                        color2 = Color.FromArgb(0xff, 0xa9, 0xc7, 0xff);
                    }
                    this.StockTopFiveChangesControls.Add(canvas);
                    histogram = new StockTopFiveHistogram {
                        HistogramAreaWidth = this.HistogramAreaWidth,
                        HistogramWidth = (num3 < 2.0) ? 2.0 : num3,
                        HistogramColor = orange,
                        HistogramLineColor = color2
                    };
                    if (this.index == 1)
                    {
                        histogram.IsLoad = true;
                    }
                    else
                    {
                        histogram.IsLoad = false;
                    }
                    canvas2 = new Canvas {
                        Width = this.HistogramAreaWidth,
                        Height = 20.0,
                        Tag = num2
                    };
                    canvas2.MouseEnter += new MouseEventHandler(this.rectCanvas_MouseEnter);
                    canvas2.MouseLeave += new MouseEventHandler(this.rectCanvas_MouseLeave);
                    canvas2.Margin = new Thickness(base.Width * 0.25, 6.0, 0.0, 0.0);
                    canvas2.Children.Add(histogram);
                    this.LayoutRoot.Children.Add(canvas2);
                    Grid.SetRow(canvas2, this.GridRowIndex);
                    Grid.SetColumn(canvas2, 0);
                    this.GridRowIndex++;
                }
            }
        }

        protected void RenderResource()
        {
            IntervalUpdates.SetRscDelegate(new PostResourceDelegate(this.PostStockTopFiveResourceByInterval));
            XElement element = (from labels in XDocument.Load("PageConfig.xml").Elements("Configs").Elements<XElement>("StockTopFiveChangesArea").Elements<XElement>("StockTopFiveChangesItem") select labels).ToList<XElement>()[0];
            this.title = element.Element("Title").Value;
            this.sellText = element.Element("Sell").Value;
            this.currentPrice = element.Element("CurrentPrice").Value;
            this.buyText = element.Element("Buy").Value;
        }

        protected void RenderTitle(string content, int rowIndex)
        {
            LinearGradientBrush brush = new LinearGradientBrush {
                StartPoint = new Point(0.5, 0.0),
                EndPoint = new Point(0.5, 1.0)
            };
            GradientStop stop = new GradientStop();
            GradientStop stop2 = new GradientStop();
            stop.Color = Color.FromArgb(0xff, 0xec, 0xff, 0xff);
            stop2.Color = Color.FromArgb(0xff, 0xae, 0xbf, 0xc7);
            stop2.Offset = 1.0;
            brush.GradientStops.Add(stop);
            brush.GradientStops.Add(stop2);
            RowDefinition definition = new RowDefinition {
                Height = new GridLength(20.0)
            };
            this.LayoutRoot.RowDefinitions.Add(definition);
            TextBlock block = new TextBlock {
                Text = content + "   " + this.priceValue,
                FontSize = 11.0,
                Foreground = new SolidColorBrush(Colors.Red)
            };
            this.LayoutRoot.Children.Add(block);
            block.Margin = new Thickness(10.0, (20.0 - block.ActualHeight) / 2.0, 0.0, 0.0);
            Grid.SetRow(block, rowIndex);
            Grid.SetColumn(block, 0);
            this.StockTopFiveChangesControls2.Add(block);
            this.GridRowIndex++;
        }

        private void StockTopFiveChanges_Loaded(object sender, RoutedEventArgs e)
        {
            this.RenderResource();
            this.RenderGridFramework();
            this.BindStockTopFiveInfo();
        }

        public List<double> BuyTopAmount5
        {
            get
            {
                return this.buyTopAmount5;
            }
            set
            {
                this.buyTopAmount5 = value;
            }
        }

        public List<string> BuyTopPrice5
        {
            get
            {
                return this.buyTopPrice5;
            }
            set
            {
                this.buyTopPrice5 = value;
            }
        }

        public List<double> SellTopAmount5
        {
            get
            {
                return this.sellTopAmount5;
            }
            set
            {
                this.sellTopAmount5 = value;
            }
        }

        public List<string> SellTopPrice5
        {
            get
            {
                return this.sellTopPrice5;
            }
            set
            {
                this.sellTopPrice5 = value;
            }
        }

        public delegate void EventHandler(object sender, MouseEventArgs e);

        public enum StockTopFiveType
        {
            Buy = 2,
            Sell = 1
        }
    }
}
